package com.mygdx.game.model;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
import com.badlogic.gdx.math.Vector2;

public class Ship {
	
	public static final int SPEED = 500;
	public static final int ACCELERATION = 10;
	
	Animation planeAnim;
	
	Vector2 planePosition = new Vector2(0,0);
	Vector2 planeVelocity = new Vector2(0,0);
	
	int planeXOffset;
	int planeYOffset;

	public Ship() {
		
		Texture frame1 = new Texture("spaceship-icon1.png");
		frame1.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		Texture frame2 = new Texture("spaceship-icon2.png");
		Texture frame3 = new Texture("spaceship-icon3.png");
		
		planeAnim = new Animation(
				0.05f, 
				new TextureRegion(frame1), 
				new TextureRegion(frame2),
				new TextureRegion(frame3), 
				new TextureRegion(frame2));
		planeAnim.setPlayMode(PlayMode.LOOP);
		
		planeYOffset = frame1.getHeight() / 2;
		planeXOffset = frame1.getWidth() / 2;
	}
	
	public void reset()
	{
		planePosition.set(100, 100);
		planeVelocity.set(0, 0);
	}
	
	public float posX() { return planePosition.x; }
	public float posY() { return planePosition.y; }
	
	public float velX() { return planeVelocity.x; }
	public float velY() { return planeVelocity.y; }
	public void setVx(float x) { planeVelocity.x = x; }
	public void setVy(float y) { planeVelocity.y = y; }
	public void subVx(float x) {	planeVelocity.x -= x; }
	public void subVy(float y) { planeVelocity.y -= y; }
	public void addVx(float x) { planeVelocity.x += x; }
	public void addVy(float y) { planeVelocity.y += y; }
	
	public float currentSpeed() {
		return planeVelocity.len();
	}
	
	public void stop() {
		planeVelocity.x = 0;
		planeVelocity.y = 0;
	}
	public boolean isStopped() { return planeVelocity.isZero(); }
	
	public void mulAddAtTime(float deltaTime) {
		planePosition.mulAdd(planeVelocity, deltaTime);
	}
	
	public TextureRegion getDrawTexture(float planeStateTime) {
		return planeAnim.getKeyFrame(planeStateTime);
	}
	
	public float getDrawX() {
		return planePosition.x - planeXOffset;
	}
	public float getDrawY() {
		return planePosition.y - planeYOffset;
	}
}
